import ReactDOM from 'react-dom'
import React, { Component } from 'react'

class App extends Component {

  state = {
    count: 0
  }

  add = () => {
    // 需求：三次setState，每一次根据上一次更新的结果继续更新
    // this.setState({ count: this.state.count + 1 })
    // this.setState({ count: this.state.count + 1 })
    // this.setState({ count: this.state.count + 1 })
    // 进行合并进行覆盖

    // setState 传入函数的写法
    this.setState(prevState => {
      return { count: prevState.count + 1 }
    })
    this.setState(prevState => {
      return { count: prevState.count + 1 }
    })
    this.setState(prevState => {
      return { count: prevState.count + 1 }
    })
    // prevState 上一次执行 setState 更新后的状态对象

    // count === 3 想要的结果
    console.log(this.state.count)
  }

  render() {
    console.log('render')
    return (
      <div className='app'>
        <div>计数器：{this.state.count}</div>
        <button onClick={this.add}>+1</button>
      </div>
    )
  }
}


ReactDOM.render(<App />, document.getElementById('root'))